﻿package baidu.reader.txt.ui
{
    import baidu.reader.common.*;
    import baidu.reader.common.model.*;
    import baidu.reader.common.model.VO.*;
    import baidu.reader.common.ui.*;
    import baidu.reader.txt.model.*;
    import baidu.reader.txt.ui.component.*;
    import baidu.ui.layout.*;
    import baidu.ui.managers.*;
    import com.greensock.*;
    import com.greensock.easing.*;
    import flash.events.*;

    public class TxtUIMain extends UIMain
    {
        private var _bookPage:TxtBookPageBase;
        private var _singleBookPage:TxtSingleBookPage;
        private var _doubleBookPage:TxtDoubleBookPage;
        private var preModel:AppPreferenceModel;
        private var configModel:AppConfigModel;
        private var chapModel:TxtChapterModel;
        private var setting_panel:SettingsPanel;
        private var previousType:String;

        public function TxtUIMain()
        {
            this.setting_panel = new SettingsPanel();
            return;
        }// end function

        override protected function initModel() : void
        {
            this.preModel = InstanceManager.createSingletonInstance(AppPreferenceModel);
            this.preModel.addEventListener(ReaderEvent.DISPLAY_TYPE_CHANGED, this.displayTypeChangeHandler);
            this.configModel = InstanceManager.createSingletonInstance(AppConfigModel);
            this.chapModel = InstanceManager.createSingletonInstance(TxtChapterModel);
            return;
        }// end function

        override protected function initToolBar() : void
        {
            super.initToolBar();
            toolbar.zoomInButton.visible = false;
            toolbar.zoomOutButton.visible = false;
            toolbar.originalButton.visible = false;
            return;
        }// end function

        private function displayTypeChangeHandler(event:ReaderEvent) : void
        {
            this.showPageContent();
            return;
        }// end function

        public function showPageContent() : void
        {
            switch(this.preModel.displayType)
            {
                case AppPreferenceVO.SINGLE_PAGE:
                {
                    this.switchToSinglePage();
                    break;
                }
                case AppPreferenceVO.DOUBLE_PAGE:
                {
                    this.switchToDoublePage();
                    break;
                }
                default:
                {
                    this.switchToSinglePage();
                    break;
                    break;
                }
            }
            hideToC();
            return;
        }// end function

        private function switchToDoublePage() : void
        {
            if (!this._doubleBookPage)
            {
                this._doubleBookPage = new TxtDoubleBookPage();
            }
            if (this._bookPage is TxtSingleBookPage)
            {
                this._bookPage.disableModelUpdate();
                removeChild(this._bookPage);
            }
            this._bookPage = this._doubleBookPage;
            this._bookPage.enableModelUpdate();
            addChild(this._bookPage);
            var _loc_1:* = this._childsLayoutInfo[this._bookPage] as LayoutInfo;
            _loc_1.left = 20;
            _loc_1.right = 20;
            _loc_1.top = 40;
            _loc_1.bottom = 30;
            toolbar.isSingleDisplay = false;
            return;
        }// end function

        private function switchToSinglePage() : void
        {
            if (!this._singleBookPage)
            {
                this._singleBookPage = new TxtSingleBookPage();
            }
            if (this._bookPage is TxtDoubleBookPage)
            {
                this._bookPage.disableModelUpdate();
                removeChild(this._bookPage);
            }
            this._bookPage = this._singleBookPage;
            this._bookPage.enableModelUpdate();
            addChild(this._bookPage);
            var _loc_1:* = this._childsLayoutInfo[this._bookPage] as LayoutInfo;
            _loc_1.left = 0;
            _loc_1.right = 20;
            _loc_1.top = 40;
            _loc_1.bottom = 30;
            toolbar.isSingleDisplay = true;
            return;
        }// end function

        override public function hidePageContent() : void
        {
            if (this._bookPage)
            {
                removeChild(this._bookPage);
            }
            return;
        }// end function

        public function get pageViewport() : TxtBookPageBase
        {
            return this._bookPage;
        }// end function

        override public function showToC(param1:Boolean = false) : void
        {
            this.closeSettingPanel();
            super.showToC(param1);
            return;
        }// end function

        public function showSettingPanel() : void
        {
            addChild(this.setting_panel);
            this.setting_panel.y = 35;
            this.setting_panel.x = stage.stageWidth - (toolbar.rightBox.width - toolbar.settingsButton.x) + toolbar.settingsButton.width / 2;
            this.setting_panel.alpha = 1;
            TweenLite.from(this.setting_panel, 0.25, {autoAlpha:0, y:-10, ease:Quad.easeOut});
            toolbar.settingsButton.enabled = false;
            return;
        }// end function

        public function closeSettingPanel() : void
        {
            if (!this.setting_panel.parent && this.setting_panel.alpha == 0)
            {
                return;
            }
            TweenLite.to(this.setting_panel, 0.2, {autoAlpha:0, y:-10, ease:Quad.easeOut});
            toolbar.settingsButton.enabled = true;
            return;
        }// end function

        override protected function fullScreenHandler(event:FullScreenEvent) : void
        {
            inputFocusOutHandler(null);
            pageNumberDisplay.enabled = !event.fullScreen;
            if (event.fullScreen)
            {
                this.previousType = this.preModel.displayType;
                dispatchEvent(new ReaderEvent(ReaderEvent.GO_DOUBLE_PAGE));
                toolbar.isSingleDisplay = false;
            }
            else if (this.previousType == AppPreferenceVO.SINGLE_PAGE)
            {
                dispatchEvent(new ReaderEvent(ReaderEvent.GO_SINGLE_PAGE));
            }
            return;
        }// end function

    }
}
